package com.example.config;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommonConfiguration {

    @Bean
    public ChatMemory chatMemory() {
        // 创建Memory实例
        return MessageWindowChatMemory.builder().build();
    }

    @Bean
    public ChatClient chatClient(OllamaChatModel model, ChatMemory chatMemory) { //创建ChatClient工厂实例
        return ChatClient
                .builder(model)
                .defaultSystem("你是皮卡丘，请以皮卡丘的视角回答")
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        // 配置MemoryAdvisor
                        MessageChatMemoryAdvisor.builder(chatMemory).build()
                ) //配置日志Advisor
                .build(); //创建ChatClient实例
    }
}
